<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="音乐标题" prop="name">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入音乐标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="音乐人" prop="artist">
        <el-input
          v-model="queryParams.artist"
          placeholder="请输入音乐人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="音乐类型" prop="genre">
        <el-input
          v-model="queryParams.genre"
          placeholder="请输入音乐类型或流派"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['video:category:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['video:category:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="音乐标题" align="center" prop="title" width="150"/>
      <el-table-column label="音乐人" align="center" prop="artist" width="150"/>
      <el-table-column label="音乐类型或流派" align="center" prop="genre" width="150"/>
      <el-table-column label="音乐发布日期" align="center" prop="releaseDate" width="180">
        <template slot-scope="scope">
          {{$yun.convertTimestamp(scope.row.releaseDate)}}
        </template>
      </el-table-column>
      <el-table-column label="音乐时长" align="center" prop="duration" width="150"/>
      <el-table-column label="音乐描述" align="center" prop="description" width="150"/>
      <el-table-column label="音频文件的URL或路径" align="center" prop="audioUrl" width="350">
        <template slot-scope="scope">
          <a @click="openMusic(scope.row.audioUrl)">{{ scope.row.audioUrl }}</a>
        </template>
      </el-table-column>
      <el-table-column label="记录创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          {{$yun.convertTimestamp(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" width="150"/>
      <el-table-column label="操作" align="center" width="150" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改短视频对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="音乐标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入音乐标题"/>
        </el-form-item>
        <el-form-item label="音乐的艺术家或演唱者" prop="artist">
          <el-input v-model="form.artist" placeholder="请输入音乐的艺术家或演唱者"/>
        </el-form-item>
        <el-form-item label="音乐类型或流派" prop="genre">
          <el-input v-model="form.genre" placeholder="请输入音乐类型或流派"/>
        </el-form-item>
        <el-form-item label="音乐发布日期" prop="releaseDate">
          <el-date-picker
            v-model="form.releaseDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择音乐发布日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="音乐时长(秒)" prop="duration">
          <el-input-number :min="0" v-model="form.duration" placeholder="请输入音乐时长(秒)"/>
        </el-form-item>
        <el-form-item label="音乐描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入音乐描述"/>
        </el-form-item>
        <el-form-item label="音频文件的URL或路径" prop="audioUrl">
          <FileUpload :limit="1" :fileType="['mp3','aac','wav','aiff','flac']" :file-size="100" v-model="form.audioUrl"></FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>

    <!-- 音乐播放-->
    <el-dialog :title="title" :visible.sync="open1" width="550px" append-to-body>
      <div v-html="audioUrl"></div>
    </el-dialog>
  </div>
</template>

<script>
import { selectAll, selectAllDel, selectAllEdit } from '@/api/video/music'
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 短视频表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        artist: null,
        genre: null,
      },
      // 表单参数
      form: {},
      audioUrl:{},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "音乐标题不能为空", trigger: "blur"}
        ],
        artist: [
          {required: true, message: "音乐的艺术家或演唱者不能为空", trigger: "blur"}
        ],
        genre: [
          {required: true, message: "音乐类型或流派不能为空", trigger: "blur"}
        ],
        releaseDate: [
          {required: true, message: "音乐发布日期不能为空", trigger: "blur"}
        ],
        duration: [
          {required: true, message: "音乐时长(秒)不能为空", trigger: "blur"}
        ],
        description: [
          {required: true, message: "音乐描述不能为空", trigger: "blur"}
        ],
        audioUrl: [
          {required: true, message: "音频文件的URL或路径不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询短视频列表 */
    getList() {
      this.loading = true;
      selectAll(this.queryParams).then(response => {
        this.categoryList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
          pageSize: 10,
          title: null,
          artist: null,
          genre: null,
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.musicId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {...row};
      this.open = true;
      this.title = "编辑";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.musicId != null) {
            selectAllEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功！");
              this.open = false;
              this.getList();
            });
          } else {
            selectAllEdit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功！");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.musicId != null ? [row.musicId] : this.ids;
      this.$modal.confirm("是否删除？").then(function () {
        return selectAllDel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功！");
      }).catch(() => {
      });
    },
    /** 音乐播放弹窗 */
    openMusic(row){
      this.open1 = true;
      this.title = '音乐播放'
      let str = this.$yun.getPreviewUrl(row)
      this.audioUrl = `<audio controls src="${str}" style="width: 500px;"></audio>`
    },
  }
};
</script>
